<?php

//应用级中间件--对/hello访问启用ip限制访问
$app->AddFunc("/hello",function() {
    echo 11;
    if ( $_SERVER['REMOTE_ADDR'] != '127.0.0.1' ) {
        $this->toHtml("not Allow!");
        return true;//返回ture,中断执行后面的路由或中间件，直接返回给浏览器
    }
});


//db操作
$app->HandleFunc("/db",function() {
    global $config;
    //$db1 = DbMysql::getInstance($config["db"]);
    //$db2 = new DbMysql($config["db"]);
    //$db3 = DbMysql::getInstance($config["db"]);
    $db1 = DbMysql::getInstance($config["db"]);
    $list = $this->db->get_all("select id from ##_user limit 1");
    //print_r($list);
    $this->toJson($list);
});

//db操作
$app->HandleFunc("/db2",function() {
    global $config;
    $swoole_mysql = new Swoole\Coroutine\MySQL();
    $swoole_mysql->connect([
        'host' => $config['db']['dbhost'],
        'port' => $config['db']['dbport'],
        'user' => $config['db']['dbuser'],
        'password' => $config['db']['dbpass'],
        'database' => $config['db']['dbname'],
    ]);
    $res = $swoole_mysql->query("select id from fa_user limit 1");
    $swoole_mysql->close();
    $this->toJson($res);
});


//注册路由api/test
$app->HandleFunc("/test",function() {
    //print_r($this->request);
    $this->toJson("test我的");
});

//自定义404
$app->on404  = function() {
    $this->toHtml("我的404");
};




/*
$app->count = 4;
$app->name = "apiServ";
//设置每个进程处理多少请求后重启(防止程序写的有问题导致内存泄露)，默认为10000
$app->max_request = 100000;
//自动加载目录--会加载目录下的所有php文件
$app->autoload = array(__DIR__."/libs/",__DIR__."/ctls/");
//启动时执行的代码，这儿包含的文件支持reload
$app->onAppStart = function($app) use($config){
    WebWorker\autoload_dir($app->autoload);
    //$app->db = new DbMysql($config["db"]);
    $app->db = DbMysql::getInstance($config["db"]);
};

//应用级中间件--对/hello访问启用ip限制访问
$app->AddFunc("/hello",function() {
    if ( $_SERVER['REMOTE_ADDR'] != '127.0.0.1' ) {
        $this->toHtml("禁止访问");
        return true;//返回ture,中断执行后面的路由或中间件，直接返回给浏览器
    }   
});
//应用级中间件--对所有以api前缀开头的启用签名验证
$app->AddFunc("/api",function() use($config) {
    $data = $_GET ? $_GET : $_POST;
    if ( !Maccess::verify_sign($data,$config["access"]) ){
        $this->toHtml("禁止访问");
        return true;
    }
});

//访问日志
//Worker::$stdoutFile = './stdout.log';
*/